package com.ww.spark.sql

import org.apache.spark.sql.{DataFrame, Dataset, SparkSession, catalog}
import org.apache.spark.sql.catalog.{Database, Table}
import org.apache.spark.{SparkConf, SparkContext}

object SQL01_basic {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setMaster("local").setAppName("sql01")
    val session = SparkSession.builder().config(conf).getOrCreate()
    val context = session.sparkContext

    //SQL为中心

    val databases: Dataset[Database] = session.catalog.listDatabases()
    databases.show()
    val tables: Dataset[Table] = session.catalog.listTables()
    tables.show()
    val functions: Dataset[catalog.Function] = session.catalog.listFunctions()
    functions.show(999,true)

    println("-----------------------------")

    val df: DataFrame = session.read.json("data/json")

    df.show()
    df.printSchema()

    df.createTempView("ooxx")   //这一个过程时df通过session 想catalog中注册表名

    //    val frame: DataFrame = session.sql("select name from ooxx")
    //    frame.show()
    //    println("----------------------")
    //    session.catalog.listTables().show()

    import scala.io.StdIn._

    while(true){
      val sql: String = readLine("input your sql: ")

      session.sql(sql).show()
    }
  }
}
